package com.qsl.ssyx.acl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.qsl.ssyx.acl.util.PermissionHelper;
import com.qsl.ssyx.model.acl.Permission;
import com.qsl.ssyx.acl.mapper.PermissionMapper;
import com.qsl.ssyx.acl.service.PermissionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 权限 服务实现类
 * </p>
 *
 * @author 青衫泪
 * @since 2023-08-08
 */
@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements PermissionService {

    //    条件分页查询-递归查询
    @Override
    public List<Permission> selectPage() {
        //    查询所有数据
        List<Permission> allPermissionList = baseMapper.selectList(null);

        // 树形数据转换
        List<Permission> permissions = PermissionHelper.buildPermission(allPermissionList);

        return permissions;
    }

    //    删除-递归删除
    @Override
    public boolean removeChildById(Integer id) {
        // 判断当前删除是否存在子数字
        LambdaQueryWrapper<Permission> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Permission::getPid, id);
        List<Permission> permissionList = baseMapper.selectList(lqw);

        // 最终要删除的Id数据
        List<Long> p = new ArrayList<>();
        p.add(Long.valueOf(id));
        for (Permission permission : permissionList) {
            Long permissionId = permission.getId();
            p.add(permissionId);
        }

        return baseMapper.deleteBatchIds(p) > 0;
    }


}
